package 学习;

import java.util.Arrays;

/**
 * Keafmd
 *
 * @ClassName: ShellSort
 * @Description: 希尔排序
 * @author: 牛哄哄的柯南
 * @date: 2021-06-24 10:39
 */
public class ShellSort {

    public static void shellSort(int[] arr) {
        shellSort(arr, true);
    }

    public static void shellSort(int[] arr, boolean ascending) {

        for (int d = arr.length / 2; d > 0; d /= 2) {

            for (int i = d; i < arr.length; i++) {
                int temp = arr[i];
                int j = 0;
                for (j = i - d; j >= 0 && (ascending ? temp < arr[j] : temp > arr[j]); j -= d) {
                    arr[j + d] = arr[j];
                }
                arr[j + d] = temp;
            }
        }
    }

    public static void main(String[] args) {
        int[] nums = {12, 4, 25, 47, 58, 34, 25, 9, 99, 26, 1, -13, 162, 10093, -66, -1};
        int[] temparr;
        //测试希尔排序
        System.out.println("测试希尔排序:");
        temparr = nums.clone();
        ShellSort.shellSort(temparr);
        //逆序排序
        //ShellSort.shellSort(temparr,false);
        System.out.println(Arrays.toString(temparr));
    }
}